package com.jfinal.weixin.sdk.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.sdk.msg.in.InTextMsg;
import com.jfinal.weixin.sdk.msg.in.event.InMenuEvent;
import com.jfinal.weixin.sdk.msg.out.News;
import com.jfinal.weixin.sdk.msg.out.OutNewsMsg;
import com.jfinal.weixin.sdk.utils.PostServer;
import com.jfinal.weixin.sdk.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 446705260@qq.com on 16/5/5.
 * chengzhichao
 */
public class TulingApi {
    /**
     * 经纬度逆解析请求地址
     *
     */
    private static String URL = "http://www.tuling123.com/openapi/api";

    private static String KEY = "5d4655464c12c8033d7e5862d675fb4c";

    public static OutNewsMsg getTulingResult(int userid, String info, InTextMsg inTextMsg){
        JSONObject json = new JSONObject();
        json.put("key", KEY);
        json.put("userid", userid);
        json.put("info", info);
        String result = PostServer.SendPost(json.toString(), "http://www.tuling123.com/openapi/api");
        JSONObject jsonObject =  JSONObject.parseObject(result);
        OutNewsMsg outNewsMsg = new OutNewsMsg(inTextMsg);
        switch (jsonObject.getInteger("code")){
            case 100000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,StringUtil.BKMURL);
                break;
            case 200000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,jsonObject.getString("url").toString());
                break;
            case 302000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,StringUtil.BKMURL);
                String listStr = jsonObject.getString("list").toString();
                JSONArray data = JSONArray.parseArray(listStr);
                List<News> list = new ArrayList();

                for (int i=0;i<7;i++){
                    jsonObject =  (JSONObject) data.get(i);
                    News news = new News();
                    news.setPicUrl(jsonObject.getString("icon"));
                    news.setTitle(jsonObject.getString("article"));
                    news.setUrl(jsonObject.getString("detailurl"));
                    list.add(news);
                }

                outNewsMsg.addNews(list);
                break;
            case 308000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),"点击这里立马查看","",jsonObject.getString("url").toString());
                break;
            default:
                outNewsMsg.addNews(jsonObject.getString("还没开发出来,怪我咯?").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,StringUtil.BKMURL);
        }
        return outNewsMsg;


    }

    public static OutNewsMsg getTulingResult(int userid, String info, InMenuEvent inTextMsg){
        JSONObject json = new JSONObject();
        json.put("key", KEY);
        json.put("userid", userid);
        json.put("info", info);
        String result = PostServer.SendPost(json.toString(), "http://www.tuling123.com/openapi/api");
        JSONObject jsonObject =  JSONObject.parseObject(result);
        OutNewsMsg outNewsMsg = new OutNewsMsg(inTextMsg);
        switch (jsonObject.getInteger("code")){
            case 100000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(), StringUtil.BKMTITLE,StringUtil.BKMIMG,StringUtil.BKMURL);
                break;
            case 200000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,jsonObject.getString("url").toString());
                break;
            case 302000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,StringUtil.BKMURL);
                String listStr = jsonObject.getString("list").toString();
                JSONArray data = JSONArray.parseArray(listStr);
                List<News> list = new ArrayList();

                for (int i=0;i<7;i++){
                    jsonObject =  (JSONObject) data.get(i);
                    News news = new News();
                    news.setPicUrl(jsonObject.getString("icon"));
                    news.setTitle(jsonObject.getString("article"));
                    news.setUrl(jsonObject.getString("detailurl"));
                    list.add(news);
                }

                outNewsMsg.addNews(list);
                break;
            case 308000:
                outNewsMsg.addNews(jsonObject.getString("text").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,jsonObject.getString("url").toString());
                break;
            default:
                outNewsMsg.addNews(jsonObject.getString("还没开发出来,怪我咯?").toString(),StringUtil.BKMTITLE,StringUtil.BKMIMG,StringUtil.BKMURL);
        }
        return outNewsMsg;


    }

}
